package huixue.cn.com.rxapp.widget;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import huixue.cn.com.rxapp.rx.CameraInterface;

/**
 * Created by gongdongyang 2019/7/19
 * Describe:
 */
public class JCaremaView extends GLSurfaceView implements SurfaceTexture.OnFrameAvailableListener{

    public JCaremaView(Context context) {
        this(context,null);


    }

    public JCaremaView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEGLContextClientVersion(2);
        setRenderer(new CameraRenderer(context));

        //GLSurfaceView使用脏模式
        //根据纹理层的监听，有数据就绘制

        setRenderMode(RENDERMODE_WHEN_DIRTY);

    }

    @Override
    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        this.requestRender();
    }

    @Override
    public void onPause() {
        super.onPause();
        CameraInterface.getInstance().doStopCamera();
    }

    class CameraRenderer implements   Renderer{

        private Context mContext;

        //以OpenGL ES纹理的形式从图像流中捕获帧,我把叫做纹理层
        private SurfaceTexture mSurfaceTextTure;

        //使用的纹理id
        int mTextureID = -1;

        DirectDrawer mDirectDrawer;

        public CameraRenderer(Context context) {
            mContext = context;

        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

            mTextureID = createAndBindVideoTexture();

            //使用这个纹理id得到纹理层SurfaceTexture
            mSurfaceTextTure = new SurfaceTexture(mTextureID);

            //监听纹理层
            mSurfaceTextTure.setOnFrameAvailableListener(JCaremaView.this);

            mDirectDrawer = new DirectDrawer(mTextureID);

            //打开相机，并未预览
            CameraInterface.getInstance().doOpenCamera();

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
            //如果还未预览，就开始预览
            if(!CameraInterface.getInstance().isPreviewing()){
                CameraInterface.getInstance().doStartPreview(mSurfaceTextTure);
            }
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
            //从图像流中将纹理图像更新为最近的帧
            mSurfaceTextTure.updateTexImage();
            mDirectDrawer.draw();
        }

        /**创建显示的texture*/
        private int createAndBindVideoTexture() {
            int[] texture = new int[1];
            //生成一个OpenGl纹理
            GLES20.glGenTextures(1,texture,0);
            //申请纹理存储区域并设置相关参数
            GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);

            GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                    GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);

            GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                    GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

            GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                    GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);

            GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                    GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);


            return texture[0];
        }
    }
}
